package com.tubeonfire.controller.admin;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tubeonfire.blobstore.BlobKeyProcess;
import com.tubeonfire.entity.Advertising;
import com.tubeonfire.model.admin.AdvertisingModel;

@SuppressWarnings("serial")
public class AddAdvertisingServlet extends HttpServlet {

	private static final Logger log = Logger
			.getLogger(AddAdvertisingServlet.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		try {
			int place = 0;
			int position = 0;
			String strPlace = req.getParameter("place");
			String strPosition = req.getParameter("position");
			try {
				place = Integer.parseInt(strPlace);
			} catch (Exception e) {
				place = 0;
			}
			try {
				position = Integer.parseInt(strPosition);
			} catch (Exception e) {
				position = 0;
			}
			if (place != 0 && position != 0) {
				HttpSession session = req.getSession();
				Advertising obj = new Advertising();
				obj.setPlace(place);
				obj.setPosition(position);
				session.setAttribute("obj", obj);
				req.getRequestDispatcher("/admin/form_advertising.jsp")
						.forward(req, resp);
			} else {
				resp.sendError(4004, "Invalid parameter !");
			}
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
			resp.sendError(4004, "We are sorry for the inconvenience ! Please try again later !");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			HttpSession session = req.getSession();
			int type = 0;
			int place = 0;
			int position = 0;
			String strType = req.getParameter("advertisingType");
			String strPlace = req.getParameter("place");
			String strPosition = req.getParameter("position");
			try {
				type = Integer.parseInt(strType);
			} catch (Exception e) {
				type = 0;
			}
			try {
				place = Integer.parseInt(strPlace);
			} catch (Exception e) {
				place = 0;
			}
			try {
				position = Integer.parseInt(strPosition);
			} catch (Exception e) {
				position = 0;
			}
			if (type == 1 && place != 0 && position != 0) {
				String content = req.getParameter("content");
				Advertising obj = new Advertising();
				obj.setId("advertising_place_" + place + "_position_"
						+ position);
				obj.setType(type);
				obj.setPlace(place);
				obj.setPosition(position);
				obj.setContent(content);
				AdvertisingModel.insert(obj);
				session.setAttribute("success", "Action success !");
				resp.sendRedirect("/admin/advertising/manage?place=" + place);
			} else if (type == 2 && place != 0 && position != 0) {
				String title = (String) req.getParameter("title");
				String link = (String) req.getParameter("link");
				String imgKey = BlobKeyProcess.getBlobKey(req, "image");

				Advertising obj = new Advertising();
				obj.setId("advertising_place_" + place + "_position_"
						+ position);
				obj.setType(type);
				obj.setPlace(place);
				obj.setPosition(position);
				obj.setTitle(title);
				obj.setLink(link);
				obj.setContent(imgKey);
				AdvertisingModel.insert(obj);
				session.setAttribute("success", "Action success !");
				resp.sendRedirect("/admin/advertising/manage?place=" + place);
			} else {
				resp.sendError(4004, "Invalid parameter !");
			}

		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
			resp.sendError(4004, "We are sorry for the inconvenience ! Please try again later !");
		}
	}
}
